$(function () {
	getmenuList();
	one();
	two();
});
var exchangeIds;
/*重置*/
function resetClick(){
    $("#Name").val("");
    $("#userState").val("");
    getmenuList();
}
/*查询*/
function getmenuList(init){
    JY.Model.loading();
	$("#ticktTable tbody").html("");
    var pageNum=$("#baseForm .pageNum").val();
    var pageSize=$("#baseForm .pageSize").val();
    var delicateMenuName = $('#Name').val();
	var userState=$("#userState option:selected").val();
	if(userState=="全部"){
        userState="";
    }
	$.ajax({
		url : jypath + "/DeclicateAskAnswer/queryMenuAdmin",
		type : "post",
		data:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "state":userState,
            "delicateMenuName":delicateMenuName,
        },
		jsonType : "json",
		success : function(data) {
		    var data=data instanceof Object ? data : JSON.parse(data);
		    console.log(data);
		    console.log("获取所有数据")
            var html="";
            var list=data.obj;
            var results=list.results;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
            if(results!=null&&results.length>0){
            	 var leng=(pageNum-1)*pageSize;//计算序号
            	 for(var i =0;i<results.length;i++){
	            	var lit = results[i];
                     var level=0;
                     html+="<tr class='tr_color'>";
	            	 html+="<td class='ver-m center'><label><input type='checkbox' name='ids' value='"+lit.delicateMenuId+"' class='ace' /> <span class='lbl'></span></label></td>";
                     html+="<td>"
                     if(lit.nodes!=null&&lit.nodes.length>0)
                         html+="<i class='icon-chevron-sign-right tree-hit tree-expanded' onclick='extendMenu(this,&apos;"+lit.delicateMenuId+"&apos;)'></i>";
                     else
                         html+="<span class='tree-indent'></span>";
                     html+=""+lit.delicateMenuName+"</td>";
                     if(lit.menuGrade == 1){
                         html+="<td class='center'>一级级别</td>";
                     }else if(lit.menuGrade == 2){
                         html+="<td class='center'>二级级别</td>";
                     }else if(lit.menuGrade == 3){
                         html+="<td class='center'>三级级别</td>";
                     }
	            	if(lit.state==1){
	                     html+="<td class='center'>有效</td>";
	                }else if(lit.state==0){
	                     html+="<td class='center'>无效</td>";
	                }
	            	 html+="<td class='center menuGrade'>"+lit.menuGrade+"</td>";
	            	html+="</tr>";
                     //递归循环
                     html=setNodes(lit.delicateMenuId,"0",lit.nodes,html,level);
	            }
	            $("#ticktTable tbody").append(html);	
	            JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getmenuList");
	        }else{
            	 html+="<tr><td colspan='7' class='center'>没有相关数据</td></tr>";
                 $("#ticktTable tbody").append(html);
                 $("#baseForm ul").empty();//清空分页
            }
            JY.Model.loadingClose();
		}
	})

}
/*
 * 设置子菜单(递归算法)
 */
function setNodes(id,parentid,nodes,html,level){
    if(nodes!=null&&nodes.length>0){
        level++;
        for(var i = 0;i<nodes.length;i++){
            var n=nodes[i];
            html+="<tr class='hide parent"+id+" parent"+parentid+" '>";
            html+="<td class='center'><label><input type='checkbox' name='ids' value='"+n.delicateMenuId+"' class='ace'/><span class='lbl'></span></label></td>";
            html+="<td>"
            if(n.nodes!=null&&n.nodes.length>0)
               html+="<i style='padding-left:15px;' class='icon-chevron-sign-right tree-hit tree-expanded' onclick='extendMenu(this,&apos;"+n.delicateMenuId+"&apos;)'></i>";
            else
                html+="<span class='tree-indent' style='padding-left:40px;'></span>";
            html+=""+n.delicateMenuName+"</td>";
            if(n.menuGrade == 1){
                html+="<td class='center'>一级级别</td>";
            }else if(n.menuGrade == 2){
                html+="<td class='center'>二级级别</td>";
            }else if(n.menuGrade == 3){
                html+="<td class='center'>三级级别</td>";
            }
            if(n.state==1){
                html+="<td class='center'>有效</td>";
            }else if(n.state==0){
                html+="<td class='center'>无效</td>";
            }
            html+="<td class='center menuGrade'>"+n.menuGrade+"</td>";
            html+="</tr>";
            //递归循环
            html=setNodes(n.delicateMenuId,n.delicateParentMenuId,n.nodes,html,level);
        }
    }
    return html;
}
/*
 * 打开子菜单
 */
function extendMenu(obj,id){
    if($(obj).hasClass('icon-chevron-sign-right')){
        $(obj).removeClass("icon-chevron-sign-right").addClass("icon-chevron-sign-down");
        $("#ticktTable .parent"+id+" i.icon-chevron-sign-right").removeClass("icon-chevron-sign-right").addClass("icon-chevron-sign-down");
        $("#ticktTable .parent"+id ).removeClass("hide");
    }else if($(obj).hasClass('icon-chevron-sign-down')){
        $(obj).removeClass("icon-chevron-sign-down").addClass("icon-chevron-sign-right");
        $("#ticktTable .parent"+id).addClass("hide");
    }
}
//查询所有二级
function two(){
	$.ajax({
        url : jypath + "/DeclicateAskAnswer/queryParentMenuAdmin",
        type : "post",
        data:{
            "delicateMenuName":$("#one_edit").find("input").val(),
            "menuGrade":2,
         },
        jsonType : "json",
        success : function(data){
            	if(data.res==1){
            		console.log(data);
            		var html = "";
            		for(var i=0;i<data.obj.length;i++){
            			 html+="<option value='"+data.obj[i].delicateMenuId+"'>"+data.obj[i].delicateMenuName+"</option>"
            		}
            		$('#twoTypeName').append(html);
            	}else{
            	console.log("获取二级失败");
            }
        }
    })
}
//查询所有一级
function one(){
	$.ajax({
        url : jypath + "/DeclicateAskAnswer/queryParentMenuAdmin",
        type : "post",
        data:{
            "delicateMenuName":$("#one_edit").find("input").val(),
            "menuGrade":1,
         },
        jsonType : "json",
        success : function(data){
            	if(data.res==1){
            		console.log(data);
            		var html = "";
            		for(var i=0;i<data.obj.length;i++){
            			 html+="<option value='"+data.obj[i].delicateMenuId+"'>"+data.obj[i].delicateMenuName+"</option>"
            		}
            		$('#oneTypeName').append(html);
            	}else{
            	console.log("获取一级失败");
            }
        }
    })
}

//;新增一级
function add1Click(){
	clearForm();
	JY.Model.edit("one_edit","新增一级菜单",function(){
			var that=$(this);
		 if(JY.Validate.form("generateForm")){
             $(".masks").removeClass("hide");
			$.ajax({
	            url : jypath + "/DeclicateAskAnswer/insertMenu",
	            type : "post",
	            data:{
	                "delicateMenuName":$("#one_edit").find("input").val(),
	                "menuGrade":1,
	             },
	            jsonType : "json",
	            success : function(data){
	                console.log("新增")
	                if(data.res==1){
	                    that.dialog("close");
                        $(".masks").addClass("hide");
	                    JY.Model.info("新增一级成功!",function(){getmenuList();});
	                }else{
	                	that.dialog("close");
                        $(".masks").addClass("hide");
	                    JY.Model.info("新增一级失败!",function(){getmenuList();});
	                }
	            }
	        })
		 }
	 })
}
//;新增二级
function add2Click(){
	clearForm();
	JY.Model.edit("two_edit","新增二级菜单",function(){
			var that=$(this);
		 if(JY.Validate.form("generateForm")){
             $(".masks").removeClass("hide");
			$.ajax({
	            url : jypath + "/DeclicateAskAnswer/insertMenu",
	            type : "post",
	            data:{
	                "parentmenuId":$("#oneTypeName").find("option:selected").val(),
	            	"delicateMenuName":$("#two_edit").find("input").val(),
	                "menuGrade":2,
	             },
	            jsonType : "json",
	            success : function(data){
	                console.log("新增")
	                if(data.res==1){
	                    that.dialog("close");
                        $(".masks").addClass("hide");
	                    JY.Model.info("新增二级成功!",function(){getmenuList();});
	                }else{
	                	that.dialog("close");
                        $(".masks").addClass("hide");
	                    JY.Model.info("新增二级失败!",function(){getmenuList();});
	                }
	            }
	        })
		 }
	 })
}
//;新增三级
function add3Click(){
	clearForm();
	JY.Model.edit("three_edit","新增三级菜单",function(){
			var that=$(this);
			if(JY.Validate.form("generateForm")){
                $(".masks").removeClass("hide");
			$.ajax({
	            url : jypath + "/DeclicateAskAnswer/insertMenu",
	            type : "post",
	            data:{
	                "delicateParentMenuId":$("#twoTypeName").find("option:selected").val(),
	            	"delicateMenuName":$("#three_edit").find("input").val(),
	                "menuGrade":3,
	             },
	            jsonType : "json",
	            success : function(data){
	                console.log("新增")
	                if(data.res==1){
	                    that.dialog("close");
                        $(".masks").addClass("hide");
	                    JY.Model.info("新增三级成功!",function(){getmenuList();});
	                }else{
	                	that.dialog("close");
                        $(".masks").addClass("hide");
	                    JY.Model.info("新增三级失败!",function(){getmenuList();});
	                }
	            }
	        })
		 }
	 })
}

//编辑 menuGrade代表级别  delicateMenuId代表id 
function editor(menuGrade,delicateMenuId){
	console.log(delicateMenuId);
	$.ajax({
        url : jypath + "/DeclicateAskAnswer/selectMenu",
        type : "post",
        data:{
            "ymDelicateMenuId":delicateMenuId,
         },
        jsonType : "json",
        success : function(data){
            console.log("编辑查数据")
            if(data.res==1){
               if(menuGrade == 1){
            	   console.log(data);
            	   $('#one_edit').find('input').val(data.obj.delicateMenuName);
            	   JY.Model.edit("one_edit","编辑一级菜单",function(){
	           		var that=$(this);
	           		 if(JY.Validate.form("generateForm")){
	           			$.ajax({
	           	            url : jypath + "/DeclicateAskAnswer/updateMenu",
	           	            type : "post",
	           	            data:{
	           	               "delicateMenuName":$("#one_edit").find("input").val(),
	           	                "delicateMenuId":delicateMenuId,
	           	             },
	           	            jsonType : "json",
	           	            success : function(data){
	           	                console.log("编辑")
	           	                if(data.res==1){
	           	                    that.dialog("close");
	           	                    JY.Model.info("编辑一级成功!",function(){getmenuList();});
	           	                }else{
	           	                	that.dialog("close");
	           	                    JY.Model.info("编辑一级失败!",function(){getmenuList();});
	           	                }
	           	            }
	           	        })
	           		 }
	           	 })
               }else if(menuGrade == 2){
            	   console.log(data);
            	   $('#oneTypeName').val(data.obj.delicateParentMenuId);
            	   $('#two_edit').find('input').val(data.obj.delicateMenuName);
            	   JY.Model.edit("two_edit","编辑二级菜单",function(){
           			var that=$(this);
	           		 if(JY.Validate.form("generateForm")){
	           			$.ajax({
	           	            url : jypath + "/DeclicateAskAnswer/updateMenu",
	           	            type : "post",
	           	            data:{
	           	                "delicateParentMenuId":$("#oneTypeName").find("option:selected").val(),
	           	            	"delicateMenuName":$("#two_edit").find("input").val(),
	           	                "delicateMenuId":delicateMenuId,
	           	             },
	           	            jsonType : "json",
	           	            success : function(data){
	           	                console.log("编辑")
	           	                if(data.res==1){
	           	                    that.dialog("close");
	           	                    JY.Model.info("编辑二级成功!",function(){getmenuList();});
	           	                }else{
	           	                	that.dialog("close");
	           	                    JY.Model.info("编辑二级失败!",function(){getmenuList();});
	           	                }
	           	            }
	           	        })
	           		 }
	           	 })
               }else if(menuGrade == 3){
            	   console.log(data);
            	   $('#twoTypeName').val(data.obj.delicateParentMenuId);
            	   $('#three_edit').find('input').val(data.obj.delicateMenuName);
            	   JY.Model.edit("three_edit","新增三级菜单",function(){
           			var that=$(this);
	           		 if(JY.Validate.form("generateForm")){
	           			$.ajax({
	           	            url : jypath + "/DeclicateAskAnswer/updateMenu",
	           	            type : "post",
	           	            data:{
	           	                "delicateParentMenuId":$("#twoTypeName").find("option:selected").val(),
	           	            	"delicateMenuName":$("#three_edit").find("input").val(),
	           	                "delicateMenuId":delicateMenuId,
	           	             },
	           	            jsonType : "json",
	           	            success : function(data){
	           	                console.log("编辑")
	           	                if(data.res==1){
	           	                    that.dialog("close");
	           	                    JY.Model.info("编辑三级成功!",function(){getmenuList();});
	           	                }else{
	           	                	that.dialog("close");
	           	                    JY.Model.info("编辑三级失败!",function(){getmenuList();});
	           	                }
	           	            }
	           	        })
	           		 }
	           	 })
               }
                
            }else{ console.log("查询失败!")}
        }
    })
}
function editClick(){
    var chks =[];
    var menuGrade="";
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
        menuGrade=$(this).parents("tr").find(".menuGrade").html();
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length==1){
        var delicateMenuId = chks[0];
        editor(menuGrade,delicateMenuId);
    }else{
        JY.Model.info("一次只能编辑一条内容!");
    }

}
//启用禁用
function enAbleClick(type){
	var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
     });
    
     if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        JY.Model.message("auForm_edit","修改状态",function(){
        	console.log(chks);
            var that = $(this);
            $.ajax({
                url : jypath + "/DeclicateAskAnswer/deleteMenu",
                type : "post",
                data:{
                    "state":states,
                    "ymDelicateMenuId":chks
                },
                jsonType : "json",
                success : function(data) {
                   console.log(data);
                   if(data.res==1){
                       that.dialog("close");
                       JY.Model.info("操作成功!",function(){getmenuList();});
                   }
                }
            })
        });
    }
}

function clearForm(){
    $("#one_edit input").val("");
    $("#two_edit input").val("");
    $('#oneTypeName').val("");
    $('#twoTypeName').val("");
    $("#three_edit input").val("");
}















